/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.tictactoe.client;

import com.dg.gaming.client.TableConnectionThread;
import com.dg.gaming.client.gui.*;
import com.dg.gaming.tictactoe.common.MsgTicTacToeMove;
import com.dg.gaming.tictactoe.common.TicTacToeModel;
import info.clearthought.layout.TableLayout;
import nanoxml.XMLElement;

// DG table frame
public class TicTacToeTableFrame extends DGTableFrame {

    private static final int WIDTH = 510, HEIGHT = 435;

    private TicTacToeModel model;
    private TicTacToeBoardComponent boardComponent;
    private TicTacToeController controller;

    // Constructor which passes a client connection thread and a table
    public TicTacToeTableFrame(TableConnectionThread conn) {
        super(conn, WIDTH, HEIGHT);

        // Create model, view and register view to model
        model = new TicTacToeModel(); // create model
        boardComponent = new TicTacToeBoardComponent(model); // board
        model.addObserver(boardComponent); // board observes model

        // Create controller which updates model and controlls the view
        controller = new TicTacToeController(model, boardComponent);
        controller.setConnection(conn); // connection
        boardComponent.setController(controller);

        // Add view to a panel and set on table frame
        double[][] sizes = { {10, TableLayout.PREFERRED, 10, TableLayout.FILL,
                           10}, {10, TableLayout.PREFERRED, TableLayout.FILL}
        };
        DGPanel panel = new DGPanel(sizes); // create a panel
        panel.add(boardComponent, "1,1,l,t"); // add board to panel
        panel.add(new PlayerComponent(conn, 0), "3,1,l,t"); // add board to panel
        panel.add(new PlayerComponent(conn, 1), "3,1,l,b"); // add board to panel
        setGamePanel(panel); // add panel to table frame

        // Set up MVC classes in super class
        setupMVC(model, boardComponent, controller);
    }

    /**
     * Recieve the table message.
     *
     * @see com.dg.gaming.client.ITable#receiveTableMessage(nanoxml.XMLElement)
     */
    public void receiveMessage(XMLElement message) {
        String messageType = message.getName();

        if (messageType.equals(MsgTicTacToeMove.XML_NAME)) {
            MsgTicTacToeMove move = new MsgTicTacToeMove(message);
            model.setData(move.getX(), move.getY(), move.getValue());
        }
    }
}
